<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="环境名称" prop="enviromentName">
        <el-input
          v-model="queryParams.enviromentName"
          placeholder="请输入环境名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="镜像状态" prop="imageStatus">
        <el-select v-model="queryParams.imageStatus" placeholder="请选择镜像状态" clearable>
          <el-option
            v-for="dict in dict.type.training_images_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作系统" prop="operationSystem">
        <el-select v-model="queryParams.operationSystem" placeholder="请选择操作系统" clearable>
          <el-option
            v-for="dict in dict.type.training_operation_system"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['training:images:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['training:images:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['training:images:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['training:images:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="imagesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="环境名称" align="center" prop="enviromentName"/>
      <el-table-column label="镜像名称" align="center" prop="imageName"/>
      <el-table-column label="镜像大小" align="center" prop="imageSize"/>
      <el-table-column label="镜像状态" align="center" prop="imageStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.training_images_status" :value="scope.row.imageStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="镜像tag" align="center" prop="tag"/>
      <el-table-column label="操作系统" align="center" prop="operationSystem">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.training_operation_system" :value="scope.row.operationSystem"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['training:images:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['training:images:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改镜像管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="环境名称" prop="enviromentName">
          <el-input v-model="form.enviromentName" placeholder="请输入环境名称"/>
        </el-form-item>
        <el-form-item label="镜像名称" prop="imageName">
          <el-input v-model="form.imageName" placeholder="请输入镜像名称"/>
        </el-form-item>
        <el-form-item label="镜像大小" prop="imageSize">
          <el-input v-model="form.imageSize" placeholder="请输入镜像大小"/>
        </el-form-item>
        <el-form-item label="镜像状态" prop="imageStatus">
          <el-select v-model="form.imageStatus" placeholder="请选择镜像状态" disabled>
            <el-option
              v-for="dict in dict.type.training_images_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜像tag" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入镜像tag"/>
        </el-form-item>
        <el-form-item label="操作系统" prop="operationSystem">
          <el-select v-model="form.operationSystem" placeholder="请选择操作系统">
            <el-option
              v-for="dict in dict.type.training_operation_system"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="镜像上传" prop="imageUrl">
          <file-upload v-model="form.imageUrl" :limit="1" :fileType="['tar','gz','zip']" :file-size="1024"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-divider content-position="center">镜像端口路径信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTrainingImagePortPath">添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTrainingImagePortPath">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="trainingImagePortPathList" :row-class-name="rowTrainingImagePortPathIndex"
                  @selection-change="handleTrainingImagePortPathSelectionChange" ref="trainingImagePortPath"
                  style="width: 100%">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="属性" prop="imageAttribute" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.imageAttribute" placeholder="请选择属性">
                <el-option
                  v-for="dict in dict.type.training_port_remark"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="端口" prop="imagePort" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.imagePort" placeholder="请输入端口"/>
            </template>
          </el-table-column>
          <el-table-column label="路径" prop="imagePath" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.imagePath" placeholder="请输入路径"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="imagePathRemark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.imagePathRemark" placeholder="请输入备注"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listImages, getImages, delImages, addImages, updateImages} from "@/api/training/images"

export default {
  name: "Images",
  dicts: ['training_operation_system', 'training_port_remark', 'training_images_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTrainingImagePortPath: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 镜像管理表格数据
      imagesList: [],
      // 镜像端口路径表格数据
      trainingImagePortPathList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enviromentName: null,
        imageStatus: null,
        operationSystem: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enviromentName: [
          {required: true, message: "环境名称不能为空", trigger: "blur"}
        ],
        imageName: [
          {required: true, message: "镜像名称不能为空", trigger: "blur"}
        ],
        imageSize: [
          {required: true, message: "镜像大小不能为空", trigger: "blur"}
        ],
        imageStatus: [
          {required: true, message: "镜像状态不能为空", trigger: "change"}
        ],
        tag: [
          {required: true, message: "镜像tag不能为空", trigger: "blur"}
        ],
        imageUrl: [
          {required: true, message: "镜像地址不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  watch: {
    form: {
      handler: function (val) {
        if (val.imageUrl) {
          this.form.imageStatus = 1
        } else {
          this.form.imageStatus = 0
        }
      },
      deep: true
    }
  },
  methods: {
    /** 查询镜像管理列表 */
    getList() {
      this.loading = true
      listImages(this.queryParams).then(response => {
        this.imagesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enviromentName: null,
        imageName: null,
        imageSize: null,
        imageStatus: null,
        tag: null,
        operationSystem: null,
        imageUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.trainingImagePortPathList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加镜像管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getImages(id).then(response => {
        this.form = response.data
        this.trainingImagePortPathList = response.data.trainingImagePortPathList
        this.open = true
        this.title = "修改镜像管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.trainingImagePortPathList = this.trainingImagePortPathList
          if (this.form.id != null) {
            updateImages(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addImages(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除镜像管理编号为"' + ids + '"的数据项？').then(function () {
        return delImages(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 镜像端口路径序号 */
    rowTrainingImagePortPathIndex({row, rowIndex}) {
      row.index = rowIndex + 1
    },
    /** 镜像端口路径添加按钮操作 */
    handleAddTrainingImagePortPath() {
      let obj = {}
      obj.imageAttribute = ""
      obj.imagePort = ""
      obj.imagePath = ""
      obj.imagePathRemark = ""
      this.trainingImagePortPathList.push(obj)
    },
    /** 镜像端口路径删除按钮操作 */
    handleDeleteTrainingImagePortPath() {
      if (this.checkedTrainingImagePortPath.length == 0) {
        this.$modal.msgError("请先选择要删除的镜像端口路径数据")
      } else {
        const trainingImagePortPathList = this.trainingImagePortPathList
        const checkedTrainingImagePortPath = this.checkedTrainingImagePortPath
        this.trainingImagePortPathList = trainingImagePortPathList.filter(function (item) {
          return checkedTrainingImagePortPath.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleTrainingImagePortPathSelectionChange(selection) {
      this.checkedTrainingImagePortPath = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('training/images/export', {
        ...this.queryParams
      }, `images_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
